package com.example.project7;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.project7.adapter.UserAdapter;
import com.example.project7.viewmodel.UserState;
import com.example.project7.viewmodel.UserViewModel;

public class UserListActivity extends AppCompatActivity {
    private static final String TAG = "UserListActivity";
    
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private TextView tvEmpty;
    private TextView tvError;
    private UserAdapter userAdapter;
    private UserViewModel userViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_list);
        
        Log.d(TAG, "onCreate");
        
        // 初始化ViewModel
        userViewModel = new ViewModelProvider(this).get(UserViewModel.class);
        
        // 初始化控件
        initViews();
        
        // 初始化RecyclerView
        initRecyclerView();
        
        // 观察用户状态
        observeUserState();
        
        // 首次加载用户列表
        loadUsers();
    }
    
    /**
     * 初始化控件
     */
    private void initViews() {
        swipeRefreshLayout = findViewById(R.id.swipe_refresh_layout);
        recyclerView = findViewById(R.id.recycler_view);
        tvEmpty = findViewById(R.id.tv_empty);
        tvError = findViewById(R.id.tv_error);
        
        // 设置下拉刷新监听器
        swipeRefreshLayout.setOnRefreshListener(() -> {
            loadUsers();
        });
    }
    
    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView() {
        userAdapter = new UserAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(userAdapter);
    }
    
    /**
     * 观察用户状态
     */
    private void observeUserState() {
        userViewModel.getUserState().observe(this, userState -> {
            if (userState instanceof UserState.Loading) {
                // 加载中状态
                showLoadingState(true);
            } else if (userState instanceof UserState.Success) {
                // 加载成功
                showLoadingState(false);
                UserState.Success successState = (UserState.Success) userState;
                userAdapter.updateData(successState.getUsers());
                showEmptyState(false);
                showErrorState(false, null);
            } else if (userState instanceof UserState.Empty) {
                // 数据为空
                showLoadingState(false);
                showEmptyState(true);
                showErrorState(false, null);
            } else if (userState instanceof UserState.Error) {
                // 加载失败
                showLoadingState(false);
                showEmptyState(false);
                UserState.Error errorState = (UserState.Error) userState;
                showErrorState(true, errorState.getErrorMessage());
            }
        });
    }
    
    /**
     * 加载用户列表
     */
    private void loadUsers() {
        userViewModel.loadUsers(1); // 加载第一页数据
    }
    
    /**
     * 显示/隐藏加载状态
     * @param show 是否显示加载状态
     */
    private void showLoadingState(boolean show) {
        if (show) {
            // 显示加载状态
            if (!swipeRefreshLayout.isRefreshing()) {
                swipeRefreshLayout.setRefreshing(true);
            }
        } else {
            // 隐藏加载状态
            swipeRefreshLayout.setRefreshing(false);
        }
    }
    
    /**
     * 显示/隐藏空数据状态
     * @param show 是否显示空数据状态
     */
    private void showEmptyState(boolean show) {
        if (show) {
            tvEmpty.setVisibility(View.VISIBLE);
        } else {
            tvEmpty.setVisibility(View.GONE);
        }
    }
    
    /**
     * 显示/隐藏错误状态
     * @param show 是否显示错误状态
     * @param errorMessage 错误信息
     */
    private void showErrorState(boolean show, String errorMessage) {
        if (show) {
            tvError.setVisibility(View.VISIBLE);
            tvError.setText(errorMessage != null ? errorMessage : "网络连接失败请稍后再试");
        } else {
            tvError.setVisibility(View.GONE);
        }
    }
}